package cn.edu.henu.team.table;

import android.database.Cursor;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.List;

import cn.edu.henu.team.application.MyApplication;
import cn.edu.henu.team.database.DaoSession;
import cn.edu.henu.team.database.health_fDao;
import cn.edu.henu.team.database.health_pDao;

public class healthfUtils {
    private static health_fDao mHealth_fDao;
    private static DaoSession daoSession;
    private static final String SQL_DISTINCT_YEAR = "SELECT DISTINCT "+health_pDao.Properties.Year.columnName+" FROM "+health_pDao.TABLENAME+" ORDER BY "+health_pDao.Properties.Year.columnName+" ASC";

    static {
        mHealth_fDao = MyApplication.getInstances().getDaoSession().getHealth_fDao();
        daoSession = MyApplication.getInstances().getDaoSession();
    }

    //增加
    public static void insert(health_f q) {
        mHealth_fDao.insertOrReplace(q);
    }

    //删除全部
    public static void deleteAll(){
        mHealth_fDao.deleteAll();
    }
    //查询全部
    public static List<health_f> loadAll() {
        return  mHealth_fDao.loadAll();
    }

    public static List queryListByYearAndMonth(int year,int month){
        QueryBuilder<health_f> qb = daoSession.queryBuilder(health_f.class);

        List<health_f> list1 = qb.where(qb.and(health_fDao.Properties.Year.eq(year), health_fDao.Properties.Month.eq(month))).list();
        return list1;
    }

    public static List<Integer> yearList(){
        ArrayList<Integer> result = new ArrayList<>();
        Cursor c = daoSession.getDatabase().rawQuery(SQL_DISTINCT_YEAR,null);
        try{
            if(c.moveToFirst()){
                do {
                    result.add(c.getInt(0));
                }while (c.moveToNext());
            }
        }finally {
            c.close();
        }
        return result;
    }
}
